<?php
/**
 * Created by PhpStorm.
 * User: Zehai.Wan
 * Date: 2018/7/9
 * Time: 14:00
 */
namespace app\api\model;
use think\Model;
use think\Request;
use think\Db;
class Preg{
    const  action ="https://aip.baidubce.com/oauth/2.0/token";
    const  client_id ="BlUcY4mh4XQpiQG9eKSGhHZC";
    const  client_secret ="9IKt7Ghp6bfjGKvbpGzoHnpKU8ucGPt4";

    //验证手机号
    public function preg_tel($tel)
    {
        $preg = '/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/';
        preg_match_all($preg, $tel, $matches);
        $pic = ($matches != null) ? $matches[0] : '';
        if($pic){
            return $pic;
        }else{
           return '';
        }
    }
    //验证邮箱
    public function preg_mail($mail)
    {
        $preg = '/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/';
        preg_match_all($preg, $mail, $matches);
        $pic = ($matches != null) ? $matches[0] : '';
        if($pic){
            return $pic;
        }else{
            return '';
        }
    }
    //验证支付密码
    public function preg_code($mail)
    {
        $preg = '/^[a-zA-Z0-9]{6}$/';
        preg_match_all($preg, $mail, $matches);
        $pic = ($matches != null) ? $matches[0] : '';
        if($pic){
            return $pic;
        }else{
            return '';
        }
    }
    function request_post($url = '', $param = array())
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        $data = curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ($code === 0) {
            throw new \Exception(curl_error($curl));
        }
        curl_close($curl);
        return $data;
    }
    //获取百度access_tooken
    public function get_tooken(){
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type']       = 'client_credentials';
        $post_data['client_id']      = self::client_id;
        $post_data['client_secret'] = self::client_secret;
        $o = "";

//        foreach ( $post_data as $k => $v )
//        {
//            $o.= "$k=" . urlencode( $v ). "&" ;
//        }
//        $post_data = substr($o,0,-1);
       // $res = $this->request_post($url, 'grant_type=client_credentials&client_id=BlUcY4mh4XQpiQG9eKSGhHZC&client_secret=9IKt7Ghp6bfjGKvbpGzoHnpKU8ucGPt4');
        $res = $this->curl($url,$post_data);
        return $res;
    }
    //百度图片鉴定
    public function get_img($token,$pic){
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/user_defined?access_token=' . $token;
        $img = file_get_contents($pic);
        $img = base64_encode($img);

        $bodys = array(
            'image' => $img
        );
        $res = $this->request_post($url, $bodys);
        return $res;
    }
    /** 替换非法字符
     * @param  String $content 要替換的字符串
     * @return String          替換后的字符串
     */
    public function replace($content){

        if(!isset($content) || $content==''){
            return '';
        }
        $str = preg_replace('/((13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8})|(([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3})|([a-zA-Z]{1}[-_a-zA-Z0-9]{5,19})/',"**",$content);//去掉电话号码、邮箱和微信号

        $a=file_get_contents('preg.txt');
        //$fw = file('a.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $arr=explode(',',$a);
        // replace black list
        if($arr){
            foreach($arr as $val){
                $str = str_replace($val, '*', $str);
            }
        }
        return $str;
    }
    //提示是否含有不文明词
    public function get_point($content){
        $a=file_get_contents('preg.txt');
        //$fw = file('a.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $arr=explode(',',$a);
        $words='';
        for($i = 0; $i < count($arr); $i++){
            $srs = stristr($content, $arr[$i]);
            if($srs !== false){
                $words.=$arr[$i].',';
                //break;
            }else{
                $words='';
            }
        }
        if($words){
            $words = substr($words,0,-1);
            $res="您输入的内容包含不文明用语'$words',请重新输入";
        }else{
            $res='';
        }
        return $res;
    }

    public function trading_excel($data){
		ini_set("memory_limit","512M");
		set_time_limit(0);
        $xlsData = $data;
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        $arrHeader = array('店铺名','商品名','交易分类','交易方式','商品描述','价格','成交量','收藏数','审核失败理由','商品上架状态(1:表示审核通过上架；2表示下架)','审核状态(1为审核通过，2为待审核，3为审核失败)','发布时间','更新时间','时间','期限','专利号','授权量');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['shop_name']);
            $objActSheet->setCellValue('B'.$k, $v['username']);
            //$objActSheet->setCellValue('C'.$k, $v['pic']);
            // 图片生成
			
			
			
            //
            $objActSheet->setCellValue('C'.$k, $v['t_name']);
            $objActSheet->setCellValue('D'.$k, $v['trading_name']);
            $objActSheet->setCellValue('E'.$k, $v['content']);
            $objActSheet->setCellValue('F'.$k, $v['store']);
            $objActSheet->setCellValue('G'.$k, $v['value']);
            $objActSheet->setCellValue('H'.$k, $v['collect']);
            $objActSheet->setCellValue('I'.$k, $v['reson']);
            $objActSheet->setCellValue('J'.$k, $v['status']);
            $objActSheet->setCellValue('K'.$k, $v['static']);
            $objActSheet->setCellValue('L'.$k, date("Y-m-d H:i:s", $v['on_time']));
            $objActSheet->setCellValue('M'.$k, date("Y-m-d H:i:s", $v['be_time']));
            $objActSheet->setCellValue('N'.$k, date("Y-m-d H:i:s", $v['time']));
            $objActSheet->setCellValue('O'.$k,  $v['update']);
            $objActSheet->setCellValue('P'.$k,  $v['order']);
            // 图片生成
				
			
            //
            $objActSheet->setCellValue('Q'.$k,  $v['volist']);
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[2]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[4]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[1]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);


        $outfile = "交易市场商品表".date("Y-m-d",time()).".xlsx";
        ob_end_clean();//linux环境去掉
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    //导出服务市场
    public function service_excel($data){
        $xlsData=$data;
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        $arrHeader = array('店铺名','商品名','商品图片','服务分类','商品描述','价格','成交量','收藏数','审核失败理由','评价数','商品上架状态(1:表示审核通过上架；2表示下架；3表示删除)','审核状态(1为审核通过，2为待审核，3为审核失败)','发布时间','更新时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['shop_name']);
            $objActSheet->setCellValue('B'.$k, $v['goods_name']);
            $objActSheet->setCellValue('C'.$k, 'Images/serviceGoods/'.$v['pic']);
            // 图片生成
            //$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            //$objDrawing[$k]->setPath('Images/serviceGoods/'.$v['pic']);
            // 设置宽度高度
            //$objDrawing[$k]->setHeight(160);//照片高度
            //$objDrawing[$k]->setWidth(160); //照片宽度
            /*设置图片要插入的单元格*/
            //$objDrawing[$k]->setCoordinates('C'.$k);
            // 图片偏移距离
            $objDrawing[$k]->setOffsetX(12);
            $objDrawing[$k]->setOffsetY(12);
            $objDrawing[$k]->setWorksheet($objExcel->getActiveSheet());
            //
            $objActSheet->setCellValue('D'.$k, $v['t_name']);
            $objActSheet->setCellValue('E'.$k, $v['content']);
            $objActSheet->setCellValue('F'.$k, $v['price']);
            $objActSheet->setCellValue('G'.$k, $v['volume']);
            $objActSheet->setCellValue('H'.$k, $v['collect']);
            $objActSheet->setCellValue('I'.$k, $v['reason']);
            $objActSheet->setCellValue('J'.$k, $v['rate']);
            $objActSheet->setCellValue('K'.$k, $v['state']);
            $objActSheet->setCellValue('L'.$k, $v['status']);
            $objActSheet->setCellValue('M'.$k, date("Y-m-d H:i:s", $v['add_time']));
            $objActSheet->setCellValue('N'.$k, date("Y-m-d H:i:s", $v['edit_time']));
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[2]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[4]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[1]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);


        $outfile = "服务市场商品表".date("Y-m-d",time()).".xlsx";
        ob_end_clean();//linux环境去掉
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    public function shop_excel($data){
        $xlsData=$data;
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        $arrHeader = array('用户名','店铺名称','商铺总成交量','店铺收藏数','店铺浏览量','手机号','邮箱','创建时间','状态(1:启用2：禁用)','禁用的理由','店铺招牌','状态(1为审核通过，2为待审核，3为审核失败)');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['username']);
            $objActSheet->setCellValue('B'.$k, $v['name']);
            $objActSheet->setCellValue('C'.$k, $v['shop_volume']);
            // 图片生成
			
				$objActSheet->setCellValue('K'.$k, 'Images/Information/'.$v['image']);
			
            //
            $objActSheet->setCellValue('D'.$k, $v['collect']);
            $objActSheet->setCellValue('E'.$k, $v['shop_view']);
            $objActSheet->setCellValue('F'.$k, $v['phone']);
            $objActSheet->setCellValue('G'.$k, $v['email']);
            $objActSheet->setCellValue('H'.$k, date("Y-m-d H:i:s", $v['time']));
            $objActSheet->setCellValue('I'.$k, $v['static']);
            $objActSheet->setCellValue('J'.$k, $v['result']);
//
            $objActSheet->setCellValue('L'.$k, $v['status']);

            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[2]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[4]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[1]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);


        $outfile = "店铺表".date("Y-m-d",time()).".xlsx";
        ob_end_clean();//linux环境去掉
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    //生成新闻表
    public function news_excel($data){
        $xlsData=$data;
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        $arrHeader = array('标题','发布人','新闻类型','新闻摘要','内容','图片','时间','浏览量','收藏数','点赞数','状态(1为正常，2为草稿，3为删除)');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['title']);
            $objActSheet->setCellValue('B'.$k, $v['username']);
            $objActSheet->setCellValue('C'.$k, $v['name']);
            // 图片生成
            
                $objActSheet->setCellValue('F'.$k, $v['pic']);
            
            //
            $objActSheet->setCellValue('D'.$k, $v['abstract']);
            $objActSheet->setCellValue('E'.$k, $v['content']);

            $objActSheet->setCellValue('G'.$k,date("Y-m-d H:i:s", $v['time']));
            $objActSheet->setCellValue('H'.$k, $v['views']);
            $objActSheet->setCellValue('I'.$k, $v['collect']);
            $objActSheet->setCellValue('J'.$k, $v['likes']);
//
            $objActSheet->setCellValue('K'.$k, $v['status']);

            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[2]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[4]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[1]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);


        $outfile = "新闻表".date("Y-m-d",time()).".xlsx";
        ob_end_clean();//linux环境去掉
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    //生成视频表
    public function video_excel($data){
        $xlsData=$data;
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        $arrHeader = array('剧集标题','发布人','视频类型','剧集封面','子视频名称','子视频简介','视频地址','视频浏览数','收藏数','视频点赞数','视频评论数','状态(1为正常，2为草稿，3为删除)','时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['title']);
            $objActSheet->setCellValue('B'.$k, $v['username']);
            $objActSheet->setCellValue('C'.$k, $v['name']);
            // 图片生成
            
                $objActSheet->setCellValue('F'.$k, $v['pic']);
            

            //
            $objActSheet->setCellValue('E'.$k, $v['v_title']);
            $objActSheet->setCellValue('F'.$k, $v['v_abstract']);
            $objActSheet->setCellValue('G'.$k, $v['content']);
            $objActSheet->setCellValue('H'.$k,$v['v_views']);
            $objActSheet->setCellValue('I'.$k, $v['v_collect']);
            $objActSheet->setCellValue('J'.$k, $v['v_likes']);
            $objActSheet->setCellValue('K'.$k, $v['comments']);
            $objActSheet->setCellValue('L'.$k, $v['status']);
            $objActSheet->setCellValue('M'.$k, date("Y-m-d H:i:s", $v['time']));
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[2]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[4]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[1]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);


        $outfile = "视频表".date("Y-m-d",time()).".xlsx";
        ob_end_clean();//linux环境去掉
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }


    //发送请求
    public function curl($action, $params,$httpMethod='POST',$contentType='urlencoded') {
        $ch = curl_init();
        $httpMethod = strtoupper($httpMethod);
        $contentType = strtolower($contentType);
        if ($httpMethod=='POST' && $contentType=='urlencoded') {
            $httpHeader[] = 'Content-Type:application/x-www-form-urlencoded';
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->build_query($params));
        }
        if ($httpMethod=='POST' && $contentType=='json') {
            $httpHeader[] = 'Content-Type:Application/json';
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params) );
        }
        if ($httpMethod=='GET' && $contentType=='urlencoded') {
            $action .= strpos($action, '?') === false?'?':'&';
            $action .= $this->build_query($params);
        }
        curl_setopt($ch, CURLOPT_URL, $action);
        curl_setopt($ch, CURLOPT_POST, $httpMethod=='POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec($ch);
        if (false === $ret) {
            $ret =  curl_errno($ch);
        }
        curl_close($ch);
        return $ret;
    }
    private function build_query($formData, $numericPrefix = '', $argSeparator = '&', $prefixKey = '') {
        $str = '';
        foreach ($formData as $key => $val) {
            if (!is_array($val)) {
                $str .= $argSeparator;
                if ($prefixKey === '') {
                    if (is_int($key)) {
                        $str .= $numericPrefix;
                    }
                    $str .= urlencode($key) . '=' . urlencode($val);
                } else {
                    $str .= urlencode($prefixKey) . '=' . urlencode($val);
                }
            } else {
                if ($prefixKey == '') {
                    $prefixKey .= $key;
                }
                if (isset($val[0]) && is_array($val[0])) {
                    $arr = array();
                    $arr[$key] = $val[0];
                    $str .= $argSeparator . http_build_query($arr);
                } else {
                    $str .= $argSeparator . $this->build_query($val, $numericPrefix, $argSeparator, $prefixKey);
                }
                $prefixKey = '';
            }
        }
        return substr($str, strlen($argSeparator));
    }
}